package com.webbei.shoppingCar.action;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SessionCheckFilter implements Filter {

	@Override
	public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain arg2)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) rq;
		HttpServletResponse response = (HttpServletResponse) rs;
		String uri = request.getRequestURI();
		if (uri.endsWith("Login.jsp") || uri.endsWith("register.jsp")
				|| uri.endsWith("test.jsp") || uri.endsWith("/")
				|| uri.endsWith("findpwd.jsp") || uri.endsWith("newPwd.jsp")
				|| uri.endsWith("webbei.jsp")|| uri.endsWith("upload_json.jsp")
				|| uri.endsWith("file_manager_json.jsp")||uri.endsWith("alipayapi.jsp")||uri.endsWith("alipayReturn.jsp")
				|| uri.endsWith("notify_url.jsp")|| uri.endsWith("getPass.jsp")) {
			arg2.doFilter(request, response);
		} else {
			Object o = request.getSession().getAttribute("username");
			if (o == null || o.toString().equals("")) {
				response.sendRedirect("showProducts.action");
			} else {
				System.out.println("��session");
				arg2.doFilter(request, response);
			}
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
}
